<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    v-if="dialogVisible"
    :close-on-click-modal="false"
    width="36.4%"
    :before-close="handleClose"
    class="add-dialog"
  >
    <div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        size="mini"
        ref="ruleForm"
        label-width="110px"
        class="demo-ruleForm"
      >
        <el-form-item label="系统名称:" prop="systemId">
          <!-- <el-input v-model="ruleForm.sysName"></el-input> -->
          <el-select
            v-model="ruleForm.systemId"
            placeholder="请选择"
            style="width: 100%"
            @change="selectSysChanged"
          >
            <el-option
              v-for="item in systemOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="环境名称:" prop="environmentId">
          <el-select
            v-model="ruleForm.environmentId"
            placeholder="请选择"
            style="width: 100%"
            @change="selectEnvChanged"
          >
            <el-option
              v-for="item in envOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务名称:" prop="serviceId">
          <el-select
            v-model="ruleForm.serviceId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in serviceOption"
              :key="item.reId"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="ruleForm.serviceName"></el-input> -->
        </el-form-item>

        <el-form-item label="接口名称:" prop="name">
          <el-input
            v-model="ruleForm.name"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>

        <el-form-item label="url" prop="url">
          <el-input
            v-model="ruleForm.url"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item label="方法:" prop="method">
          <el-select
           @change="selectMethodChanged"
            v-model="ruleForm.method"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in methodOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input
            v-model="ruleForm.method"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input> -->
        </el-form-item>
        <el-form-item label="参数类型:" prop="parameterType">
          <el-select
            v-model="ruleForm.parameterType"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in parameterOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input
            v-model="ruleForm.parameterType"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input> -->
        </el-form-item>
        <el-form-item label="参数:" prop="parameter" v-if="ruleForm.method!=1&&ruleForm.parameterType!=2">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            v-model="ruleForm.parameter"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item label="header参数:" prop="header">
          <!-- <el-input
          style="width: 50px;display: none;"
            v-model="ruleForm.header"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input> -->
           
          <el-button type="text" @click="editHeader">编辑header参数</el-button>
          <!-- <span class="add-header">
            编辑header参数
          </span> -->
        </el-form-item>
        <el-form-item label="调用成功标准:" prop="responseSuccessStandard">
          <el-select
            v-model="ruleForm.responseSuccessStandard"
            placeholder="请选择"
            style="width: 100%"
            @change="standardChange"
          >
            <el-option
              v-for="item in standardOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input
            v-model="ruleForm.responseSuccessStandard"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input> -->
        </el-form-item>
        <el-form-item
          label="正则表达式:"
          prop="responseSuccessRegularExpression"
          v-if="ruleForm.responseSuccessStandard==1"
        >
          <el-input
            v-model="ruleForm.responseSuccessRegularExpression"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
// import EditHeader from './components/EditHeader.vue'
export default {
  props: {
    systemOption: {
      type: Array,
      default: () => [],
    },
    envOption: {
      type: Array,
      default: () => [],
    },
    serviceOption: {
      type: Array,
      default: () => [],
    },
  },
  components: {},
  data() {
    return {
      title: "新增接口",
      dialogVisible: false,
      methodOption: [
        { id: 1, name: "GET" },
        { id: 2, name: "POST" },
      ],
      parameterOption: [
        { id: 1, name: "form中的参数" },
        { id: 2, name: "无" },
        { id: 3, name: "body中的参数" },
      ],
      standardOption: [
        { id: 1, name: "正则表达式" },
        { id: 2, name: "文件下载成功" },
      ],
      ruleForm: {
        id: "",
        serviceId: "",
        systemId: "",
        environmentId: "",
        name: "",
        url: "",
        method: "",
        parameterType: "",
        parameter: "",
        header: "",
        responseSuccessStandard: "",
        responseSuccessRegularExpression: "",
      },
      rules: {
        systemId: [
          { required: true, message: "请选择系统名称", trigger: "change" },
        ],
        environmentId: [
          { required: true, message: "请选择环境名称", trigger: "change" },
        ],
        serviceId: [
          { required: true, message: "请选择服务名称", trigger: "change" },
        ],
        name: [{ required: true, message: "请输入接口名称", trigger: "blur" }],
        url: [{ required: true, message: "请输入URL", trigger: "blur" }],
        method: [{ required: true, message: "请选择方法", trigger: "change" }],
        parameterType: [
          { required: true, message: "请选择参数类型", trigger: "change" },
        ],
        responseSuccessStandard: [
          { required: true, message: "请选择调用成功标准", trigger: "change" },
        ],
         responseSuccessRegularExpression: [{ required: true, message: "请输入接口名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    standardChange(){
      if(this.ruleForm.responseSuccessStandard==2){
        this.ruleForm.responseSuccessRegularExpression="";
      }
    },
    selectMethodChanged(){
       if(this.ruleForm.method==1){
        this.ruleForm.parameterType=2;
       }else{
        this.ruleForm.parameterType="";
       }
    },
    setHeaderData(data){
       this.ruleForm.header=JSON.stringify(data);
    },
    editHeader(){
       this.$emit("editHeader", this.ruleForm.header);
    },
    clearSelectValue(type) {
      if (type == 1) {
        this.ruleForm.environmentId = "";
      } else {
        this.ruleForm.serviceId = "";
      }
    },
    selectSysChanged() {
      this.$emit("selectSysChanged", this.ruleForm.systemId);
      this.ruleForm.environmentId = "";
    },
    selectEnvChanged() {
      this.$emit(
        "selectEnvChanged",
        this.ruleForm.environmentId,
        this.ruleForm.systemId
      );
      this.ruleForm.serviceId = "";
    },
    open(data) {
      this.title = "新增接口";
      if (data) {
        this.title = "编辑接口";
        this.ruleForm = Object.assign(this.ruleForm, data);
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    close() {
      this.resetForm("ruleForm");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("save", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.ruleForm = {
        id: "",
        serviceId: "",
        systemId: "",
        environmentId: "",
        name: "",
        url: "",
        method: "",
        parameterType: "",
        parameter: "",
        header: "",
        responseSuccessStandard: "",
        responseSuccessRegularExpression: "",
      };
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
      this.$emit("updateType");
    },
  },
  created() {},
};
</script>
<style lang="scss" scoped>

.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
